﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace System.Serialization
{
    public static partial class SerializationManager
    {
        #region 二进制序列化

        /// <summary>
        /// 二进制序列化
        /// </summary>
        /// <param name="this">要序列化的对象</param>
        /// <returns>byte[]</returns>
        public static byte[] BinarySerialize(this object @this)
        {
            var serializer = new BinaryFormatter();
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(stream, @this);
                return stream.GetBuffer();
            }
        }

        /// <summary>
        /// 反序列化二进制对象
        /// </summary>
        /// <typeparam name="T">要二进制反序列成的类型</typeparam>
        /// <param name="this"></param>
        /// <returns></returns>
        public static T BinaryDeSerialize<T>(this byte[] @this)
        {
            var deserializer = new BinaryFormatter();
            using (var stream = new MemoryStream(@this))
            {
                return (T)deserializer.Deserialize(stream);
            }
        }
        /// <summary>
        /// 反序列化二进制对象
        /// </summary>
        /// <typeparam name="T">要二进制反序列成的类型</typeparam>
        /// <param name="this"></param>
        /// <returns></returns>
        public static T BinaryDeSerialize<T>(this Stream @this)
        {
            var deserializer = new BinaryFormatter();
            return (T)deserializer.Deserialize(@this);
        }

        #endregion

        #region XML序列化

        /// <summary>
        /// XML序列化
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] XmlSerializer(object data)
        {
            var xs = new XmlSerializer(data.GetType());
            using (var stream = new MemoryStream())
            {
                xs.Serialize(stream, data);
                return stream.GetBuffer();
            }
        }

        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <typeparam name="T">要XML反序列化成的类型</typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T XmlDeSerialize<T>(byte[] data) where T : class
        {
            var xs = new XmlSerializer(typeof(T));
            using (var stream = new MemoryStream(data))
            {
                return xs.Deserialize(stream) as T;
            }
        }

        #endregion

        #region Soap 序列化

        /// <summary>
        /// Soap序列化
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] SoapSerialize(object data)
        {
            var serializer = new SoapFormatter();
            using (var stream = new MemoryStream())
            {
                serializer.Serialize(stream, data);
                return stream.GetBuffer();
            }
        }

        /// <summary>
        /// Soap 反序列化
        /// </summary>
        /// <typeparam name="T">要Soap反序列化成的类型</typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T SoapDeSerialize<T>(byte[] data) where T : class
        {
            var deserializer = new SoapFormatter();
            using (var stream = new MemoryStream(data))
            {
                return deserializer.Deserialize(stream) as T;
            }
        }

        #endregion
    }
}
